package com.example.assignment4.controller;

import com.example.assignment4.entity.Commodity;
import com.example.assignment4.service.CommodityService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author guanruwang
 * @date 2023/10/11$
 */
@RestController
public class CommodityController {
    @Autowired
    CommodityService commodityService;

    @GetMapping("/{id}")
    public ResponseEntity<Commodity> getTodo(@ApiParam("商品Id") @PathVariable long id) {
        Commodity result = commodityService.getCommodity(id);
        if (result == null) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.ok(result);
        }
    }


    @GetMapping("")
    public ResponseEntity<List<Commodity>> findTodos(@ApiParam("待办事项名称") String name, @ApiParam("是否完成") String description) {
        List<Commodity> result = commodityService.findCommodities(name, description);
        return ResponseEntity.ok(result);
    }

    @PostMapping("")
    public ResponseEntity<Commodity> addTodo(@RequestBody Commodity todo) {
        Commodity result = commodityService.addCommodity(todo);
        return ResponseEntity.ok(result);
    }


    @PutMapping("/{id}")
    public ResponseEntity<Void> updateTodo(@PathVariable long id, @RequestBody Commodity todo) {
        commodityService.updateCommodity(id, todo);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteTodo(@PathVariable long id) {
        commodityService.deleteCommodity(id);
        return ResponseEntity.ok().build();
    }
}
